<template>
	<view class="pb50">
		<uni-card spacing="0" margin="0" :is-shadow="false" :border="false">
			<cforms ref="formsRef" :formData="detailsData" />
			<uni-section title="活动附件图片" type="line">
				<acImage v-model:imageList="imageList" :maxNumber="9" :editShow="true" @uploadImg="uploadImg" @imgDelete="imgDelete" />
			</uni-section>
		</uni-card>
	</view>

	<view class="sticky-b0 p10">
		<button @click="submit" type="default" class="basicbutton m10" hover-class="basicbutton-hover">编辑确认</button>
	</view>
</template>

<script lang="ts" setup>
	import acImage from "@/pages/student/user/a-components/ac-image.vue";
	import cforms from "./components/c-forms.vue";
	import { SchoolActivities } from "@/api/tables/SchoolActivities"
	import {getUser } from "@/utils/storage"
	import { schoolActivitiesDetail, schoolActivitiesUpload, schoolActivitiesFileDelete} from "@/api/user"

	import { ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	
	let userData = getUser()
	let itemID = ref('')
	let detailsData = ref<SchoolActivities>({});
	let imageList = ref([])
	let formsRef = ref(null)

	onLoad((option) => {
		itemID.value = option.itemID || ''
		getDetail(option.itemID, option.jobseekerId)
	});
	
	function getDetail(inSchoolActivity, jobseekerId) {
		schoolActivitiesDetail({ inSchoolActivity, jobseekerId }).then(res => {
			if (res.code == '200') {
				let list = res.data.activityList || []
				list.forEach(item => {
					item.url = item.thumbnail
				})
				imageList.value = list
				detailsData.value = res.data
			}
		})
	}
	// 上传图片
	function uploadImg(filePath) {
		schoolActivitiesUpload({
			filePath: filePath,
			formData: {
				jobseekerId: userData.userId,
				inSchoolActivityId: itemID.value
			},
		}).then(res => {
			let list = res.data || []
			list.forEach(item => {
				item.url = item.thumbnail
			})
			imageList.value = list
		}).catch(err => {
		})
	}
	// 删除图片
	function imgDelete(id) {
		schoolActivitiesFileDelete({
			attachmentId: id,
			jobseekerId: userData.userId,
			inSchoolActivityId: itemID.value
		}).then(res => {
			if (res.code == '200') {
				let list = imageList.value
				let num = list.findIndex(v => v.id === id);
				list.splice(num, 1);
				imageList.value = list
			}
		}).catch(err => {
		})
	}
	function submit() {
		formsRef.value.formCheck()
	}
	
</script>

<style lang="scss" scoped>
	
</style>